<template>
    <div class="profile">
      <div class="page-header">
        <h2>个人信息</h2>
        <p>个人资料管理</p>
      </div>
      
      <el-card class="profile-card">
        <div class="profile-content">
          <!-- 左侧头像区域 -->
          <div class="avatar-section">
            <el-avatar 
              :size="120" 
              :src="userStore.userInfo.avatar"
              class="profile-avatar"
            >
              {{ userStore.userInfo.realName?.charAt(0) || 'U' }}
            </el-avatar>
          </div>
  
          <!-- 右侧信息区域 -->
          <div class="info-section">
            <!-- 基本信息 -->
            <div class="info-group">
              <label class="info-label">姓名</label>
              <el-input 
                v-model="profileForm.realName" 
                placeholder="姓名"
                class="info-input"
                disabled
              />
              <span class="field-note"></span>
            </div>
  
            <div class="info-group">
              <label class="info-label">身份</label>
              <el-input 
                v-model="profileForm.roleName" 
                placeholder="身份"
                class="info-input"
                disabled
              />
              <span class="field-note"></span>
              </div>
  
            <!-- 负责项目 -->
            <div class="project-section">
              <h3 class="section-title">负责项目</h3>
              <div class="project-options">
                <el-radio-group v-model="profileForm.responsibleProjects">
                  <div class="project-option">
                    <el-radio value="projectA">项目A</el-radio>
                  </div>
                  <div class="project-option">
                    <el-radio value="projectB">项目B</el-radio>
                  </div>
                  <div class="project-option">
                    <el-radio value="projectC">项目C</el-radio>
                  </div>
                </el-radio-group>
              </div>
            </div>
  
            <!-- 操作按钮 -->
            <div class="action-buttons">
              <el-button 
                type="danger" 
                @click="handleLogout"
                class="logout-btn"
              >
                退出
              </el-button>
              <el-button 
                type="primary" 
                @click="handleChangePassword"
                class="change-password-btn"
              >
                修改密码
              </el-button>
            </div>
          </div>
        </div>
      </el-card>
  
      <!-- 修改密码对话框 -->
      <el-dialog 
        v-model="passwordDialogVisible" 
        title="修改密码" 
        width="400px"
        :close-on-click-modal="false"
      >
        <el-form 
          ref="passwordFormRef" 
          :model="passwordForm" 
          :rules="passwordRules"
          label-width="100px"
        >
          <el-form-item label="原密码" prop="oldPassword">
            <el-input 
              v-model="passwordForm.oldPassword" 
              type="password" 
              placeholder="请输入原密码"
              show-password
            />
          </el-form-item>
          <el-form-item label="新密码" prop="newPassword">
            <el-input 
              v-model="passwordForm.newPassword" 
              type="password" 
              placeholder="请输入新密码"
              show-password
            />
          </el-form-item>
          <el-form-item label="确认密码" prop="confirmPassword">
            <el-input 
              v-model="passwordForm.confirmPassword" 
              type="password" 
              placeholder="请再次输入新密码"
              show-password
            />
          </el-form-item>
        </el-form>
        <template #footer>
          <span class="dialog-footer">
            <el-button @click="passwordDialogVisible = false">取消</el-button>
            <el-button type="primary" @click="submitPasswordChange">确定</el-button>
          </span>
        </template>
      </el-dialog>
    </div>
  </template>
  
  <script setup>
  import { ref, reactive, onMounted } from 'vue'
  import { useRouter } from 'vue-router'
  import { ElMessage, ElMessageBox } from 'element-plus'
  import { useUserStore } from '@/stores/user'
  
  const router = useRouter()
  const userStore = useUserStore()
  
  // 个人信息表单
  const profileForm = reactive({
    realName: '',
    roleName: '',
    responsibleProjects: ''
  })
  
  // 修改密码表单
  const passwordForm = reactive({
    oldPassword: '',
    newPassword: '',
    confirmPassword: ''
  })
  
  // 密码验证规则
  const passwordRules = {
    oldPassword: [
      { required: true, message: '请输入原密码', trigger: 'blur' }
    ],
    newPassword: [
      { required: true, message: '请输入新密码', trigger: 'blur' },
      { min: 6, message: '密码长度不能少于6位', trigger: 'blur' }
    ],
    confirmPassword: [
      { required: true, message: '请确认新密码', trigger: 'blur' },
      {
        validator: (rule, value, callback) => {
          if (value !== passwordForm.newPassword) {
            callback(new Error('两次输入的密码不一致'))
          } else {
            callback()
          }
        },
        trigger: 'blur'
      }
    ]
  }
  
  // 对话框显示状态
  const passwordDialogVisible = ref(false)
  const passwordFormRef = ref()
  
  // 初始化个人信息
  onMounted(() => {
    // 从用户store获取姓名和身份，这些字段是只读的
    profileForm.realName = userStore.userInfo.realName || '未设置'
    profileForm.roleName = userStore.userInfo.roleName || '未设置'
    
    // 根据用户角色设置默认负责项目
    if (userStore.isProjectLeader) {
      profileForm.responsibleProjects = 'projectA'
    } else if (userStore.isFrontendLeader) {
      profileForm.responsibleProjects = 'projectB'
    } else if (userStore.isBackendLeader) {
      profileForm.responsibleProjects = 'projectC'
    } else if (userStore.isDeveloper) {
      profileForm.responsibleProjects = 'projectC'
    } else {
      // 部长可以负责所有项目
      profileForm.responsibleProjects = 'projectA'
    }
  })
  
  // 退出登录
  const handleLogout = async () => {
    try {
      await ElMessageBox.confirm('确定要退出登录吗？', '提示', {
        confirmButtonText: '确定',
        cancelButtonText: '取消',
        type: 'warning'
      })
      userStore.logout()
      router.push('/login')
      ElMessage.success('已退出登录')
    } catch {
      // 用户取消
    }
  }
  
  // 修改密码
  const handleChangePassword = () => {
    passwordDialogVisible.value = true
    // 重置表单
    passwordForm.oldPassword = ''
    passwordForm.newPassword = ''
    passwordForm.confirmPassword = ''
  }
  
  // 提交密码修改
  const submitPasswordChange = async () => {
    try {
      await passwordFormRef.value.validate()
      
      // 这里应该调用API修改密码
      // 暂时模拟成功
      ElMessage.success('密码修改成功')
      passwordDialogVisible.value = false
      
      // 重置表单
      passwordForm.oldPassword = ''
      passwordForm.newPassword = ''
      passwordForm.confirmPassword = ''
    } catch (error) {
      console.error('密码修改失败:', error)
    }
  }
  </script>
  
  <style scoped>
  .profile {
    padding: 0;
  }
  
  .page-header {
    margin-bottom: 20px;
  }
  
  .page-header h2 {
    color: #303133;
    margin-bottom: 8px;
  }
  
  .page-header p {
    color: #606266;
    font-size: 14px;
  }
  
  .profile-card {
    background: white;
    border-radius: 8px;
    box-shadow: 0 2px 12px 0 rgba(0, 0, 0, 0.1);
  }
  
  .profile-content {
    display: flex;
    gap: 40px;
    padding: 20px;
  }
  
  .avatar-section {
    display: flex;
    flex-direction: column;
    align-items: center;
    gap: 16px;
  }
  
  .profile-avatar {
    border: 4px solid #e1f5fe;
    background: #4a90e2;
    color: white;
    font-size: 48px;
    font-weight: bold;
  }
  
  .info-section {
    flex: 1;
    display: flex;
    flex-direction: column;
    gap: 24px;
  }
  
  .info-group {
    display: flex;
    align-items: center;
    gap: 16px;
  }
  
  .info-label {
    width: 80px;
    font-size: 14px;
    color: #606266;
    font-weight: 500;
  }
  
  .info-input {
    flex: 1;
    max-width: 300px;
  }
  
  .info-input.is-disabled {
    background-color: #f5f7fa;
    color: #606266;
    cursor: not-allowed;
  }
  
  .field-note {
    font-size: 12px;
    color: #909399;
    margin-left: 12px;
    font-style: italic;
  }
  
  .project-section {
    margin-top: 16px;
  }
  
  .section-title {
    font-size: 16px;
    color: #303133;
    margin-bottom: 16px;
    font-weight: 500;
  }
  
  .project-options {
    display: flex;
    flex-direction: column;
    gap: 12px;
  }
  
  /* 让单选组纵向排列 */
  .project-options :deep(.el-radio-group) {
    display: flex;
    flex-direction: column;
    align-items: flex-start;
    gap: 12px;
  }
  
  .project-option {
    display: flex;
    align-items: center;
    padding: 12px 16px;
    background: #f5f7fa;
    border-radius: 6px;
    border: 1px solid #e4e7ed;
    width: 360px;
  }
  
  .project-option:hover {
    background: #ecf5ff;
    border-color: #409eff;
  }
  
  .action-buttons {
    display: flex;
    gap: 16px;
    margin-top: 24px;
  }
  
  .logout-btn {
    background: #f56c6c;
    border-color: #f56c6c;
  }
  
  .logout-btn:hover {
    background: #f78989;
    border-color: #f78989;
  }
  
  .change-password-btn {
    background: #4a90e2;
    border-color: #4a90e2;
  }
  
  .change-password-btn:hover {
    background: #66a6e8;
    border-color: #66a6e8;
  }
  
  .dialog-footer {
    display: flex;
    justify-content: flex-end;
    gap: 12px;
  }
  
  /* 响应式设计 */
  @media (max-width: 768px) {
    .profile-content {
      flex-direction: column;
      gap: 24px;
    }
    
    .info-group {
      flex-direction: column;
      align-items: flex-start;
      gap: 8px;
    }
    
    .info-input {
      max-width: 100%;
    }
    
    .action-buttons {
      flex-direction: column;
    }
    
    /* 小屏下项目选项占满宽度 */
    .project-option {
      width: 100%;
    }
  }
  </style>